package com.util;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;

public class JDBCUtil01 {
    private static String DRIVER;//（大写）业内规则：表示不再修改
    private static String URL;
    private static String USERNAME;
    private static String PASSWORD;
    //读取配置文件  为了保证读取一次  写在静态代码块中
    static {
        try {
            Properties properties = new Properties();
            InputStream is = JDBCUtil01.class.getClassLoader().getResourceAsStream("mysql.properties");
            properties.load(is);
            DRIVER=properties.getProperty("driver");
            URL=properties.getProperty("url");
            USERNAME=properties.getProperty("username");
            PASSWORD=properties.getProperty("password");
            Class.forName(DRIVER);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //获取连接的方法
    public static Connection getConnection()throws Exception{
        Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
        return connection;
    }
    //关闭
    public static void closeALL(PreparedStatement ps,Connection connection)throws Exception{
        ps.close();
        connection.close();
    }
    public static void closeALL(ResultSet rs,PreparedStatement ps,Connection connection)throws Exception{
        rs.close();
        ps.close();
        connection.close();
    }
}
